const path = require('path');
const HtmlWebpackPlugin = require("html-webpack-plugin");
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
const Webpack = require('webpack'); //引入webpack模块
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const MyPlugin = require('./webpackPlugin/index')
/**
 * webpack插件将打包好的文件注入到html模板里
 * @type {HtmlWebpackPlugin}
 */
const htmlWebpackPlugin = new HtmlWebpackPlugin({
  template: path.join(__dirname, "./public/index.html"),
  filename: "./index.html"
});

const devServer = {
  port: 3001, //端口号
  open: true, // 自动打开浏览器
  compress: true, // 启动gzip压缩
  hot: true,
  inline: true
}

module.exports = {
  mode: 'development',
  entry: path.join(__dirname, "./public/app.js"),
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: '[name].bundle.js'
  },
  module: {
    rules: [{
        test: /\.js$|.jsx/,
        use: "babel-loader",
        exclude: /node_modules/
      }, {
        test: /\.ts$|.tsx/,
        use: ['babel-loader', 'ts-loader'],
        exclude: /node_modules/
      },
      {
        test: /\.css$/,
        use: [{
            loader: "style-loader"
          },
          {
            loader: "css-loader"
          }
        ], //打包处理css样式表的第三方loader
      }, {
        //只为less启用模块化
        test: /\.less$/,
        use: [{
            loader: "style-loader"
          },
          {
            loader: "css-loader",
            options: {
              modules: {
                localIdentName: "[path][name]-[local]-[hash:5]"
              }
            }
          },
          {
            loader: "less-loader"
          },
        ]
      },
      {
        test: /\.(jpg|png|jpeg|gif)$/,
        loader: "url-loader"
      }
    ]
  },
  plugins: [
    htmlWebpackPlugin,
    new Webpack.HotModuleReplacementPlugin(),
    // new BundleAnalyzerPlugin(),
    // new MyPlugin(),
    new FriendlyErrorsWebpackPlugin()

    // new Webpack.NamedModulesPlugin()  //插件：自动注入编译打包好的文件
  ],
  resolve: {
    extensions: [".js", ".jsx", ".tsx"]
  },
  devServer: devServer,
  // optimization: {
  //   splitChunks: {
  //     chunks: 'all',
  //   },
  // },
};